package com.bykj.taketurns.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bykj.taketurns.R;
import com.bykj.taketurns.utils.Constant;
import com.dou361.ijkplayer.adapter.RewardGridAdapter;

import java.io.File;
import java.io.FileInputStream;
import java.text.DecimalFormat;

/**
 * Created by Administrator on 2016/3/21.
 */
public class EditVideoActivity extends BaseActivity {
    public static final int SIZETYPE_B = 1;// 获取文件大小单位为B的double值
    public static final int SIZETYPE_KB = 2;// 获取文件大小单位为KB的double值
    public static final int SIZETYPE_MB = 3;// 获取文件大小单位为MB的double值
    public static final int SIZETYPE_GB = 4;// 获取文件大小单位为GB的double值

    private TextView eimgxie;
    private EditText edit_content;
    private String videoPath;
    private String framePicPath;
    private ImageView imageviewvideo;
    private GridView gridView;
    private String bitmapName;
    private ImageView mVideoPlayImg;
    private TextView mEvPreviewFlTv;
    private ImageView eback;

    @Override
    public void initView(Bundle savedInstanceState) {
        TextView enametv = (TextView) findViewById(R.id.enametv);
        gridView = findViewById(R.id.et_grid);
        videoPath = getIntent().getStringExtra("videoPath");
        framePicPath = getIntent().getStringExtra("framePicPath");
        bitmapName = getIntent().getStringExtra(Constant.EXTRA_FILE_NAME);
        FrameLayout framelayoutvideo = (FrameLayout) findViewById(R.id.framelayoutvideo);
        imageviewvideo = (ImageView) findViewById(R.id.imageviewvideo);
        mVideoPlayImg = (ImageView) setViewById(R.id.videoplayimg);
        mEvPreviewFlTv = (TextView) setViewById(R.id.ev_preview_fl_tv);
        eback = (ImageView) findViewById(R.id.eback);
        edit_content = (EditText) findViewById(R.id.et_sendmessage);
        //点击监听返回按钮
        eback.setOnClickListener(this);
        eimgxie = (TextView) findViewById(R.id.eimgxie);
        //点击监听发布按钮
        eimgxie.setOnClickListener(this);
        //点击监听预览按钮
        mEvPreviewFlTv.setOnClickListener(this);
        if(!TextUtils.isEmpty(bitmapName)){
            imageviewvideo.setImageURI(Uri.parse(bitmapName));
            mVideoPlayImg.setVisibility(View.GONE);
        }else {
            mVideoPlayImg.setVisibility(View.VISIBLE);
            Glide.with(EditVideoActivity.this).load(framePicPath).error(R.drawable.jc_play_normal).into(imageviewvideo);
        }



    }

    @Override
    public void initData(Bundle savedInstanceState) {
        setAdapter();

    }

    @Override
    public int getParentView() {
        return R.layout.activity_editvideo;
    }

    @Override
    public void initToolBar() {

    }

    @Override
    protected void postData() {

    }

    //适配数据
    private void setAdapter() {
        Resources resources = getResources();
        String[] array = resources.getStringArray(R.array.comment);
        int []img=new int[]{R.drawable.qq,R.drawable.zone,R.drawable.wchat,R.drawable.friends,R.drawable.sina};
        RewardGridAdapter adapter= new RewardGridAdapter(this)
        {
            @Override
            public int getImgId() {
                return R.id.comment_item_img;
            }

            @Override
            public int getTvId() {
                return R.id.comment_item_tv;
            }

            @Override
            public View getRes() {
                return View.inflate(EditVideoActivity.this,R.layout.comment_item,null);
            }
        };
        adapter.setData(array,img);
        gridView.setAdapter(adapter);

    }


    /**
     * 转换文件大小,指定转换的类型
     *
     * @param fileS
     * @param sizeType
     * @return
     */
    private static double FormetFileSize(long fileS, int sizeType) {
        DecimalFormat df = new DecimalFormat("#.00");
        double fileSizeLong = 0;
        switch (sizeType) {
            case SIZETYPE_B:
                fileSizeLong = Double.valueOf(df.format((double) fileS));
                break;
            case SIZETYPE_KB:
                fileSizeLong = Double.valueOf(df.format((double) fileS / 1024));
                break;
            case SIZETYPE_MB:
                fileSizeLong = Double.valueOf(df.format((double) fileS / 1048576));
                break;
            case SIZETYPE_GB:
                fileSizeLong = Double.valueOf(df
                        .format((double) fileS / 1073741824));
                break;
            default:
                break;
        }
        return fileSizeLong;
    }


    /**
     * 获取指定文件大小
     *
     * @param
     * @return
     * @throws Exception
     */
    private static long getFileSize(File file) throws Exception {
        long size = 0;
        if (file.exists()) {
            FileInputStream fis = null;
            fis = new FileInputStream(file);
            size = fis.available();
        } else {
            file.createNewFile();
            Log.e("获取文件大小", "文件不存在!");
        }
        return size;
    }
    //上传视频

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.eback:
                deletePng();
                finish();
                break;
            case R.id.ev_preview_fl_tv:
                //跳转
                if (!TextUtils.isEmpty(videoPath)) {

                    Intent intent = new Intent(EditVideoActivity.this, WidthMatchVideo.class);
                    intent.putExtra("videoPath", videoPath);
                    startActivity(intent);
                } else if(!TextUtils.isEmpty(bitmapName)){
                    Intent intent = new Intent(EditVideoActivity.this, ReadWordActivity.class);
                    intent.putExtra(Constant.EXTRA_FILE_NAME, bitmapName);
                    startActivity(intent);
                }else {
                    Toast.makeText(EditVideoActivity.this, "无法展示", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.eimgxie:
                if (TextUtils.isEmpty(videoPath) || TextUtils.isEmpty(framePicPath)||TextUtils.isEmpty(bitmapName)) {
                    Toast.makeText(EditVideoActivity.this, "发布失败，请重新进行", Toast.LENGTH_SHORT).show();
                }
                //提交
                break;

        }
    }
    //删除可能存在的文件
    public  void deletePng(){
        //退出时删除录制的视频和图片
        if (!TextUtils.isEmpty(videoPath)) {
            File file = new File(videoPath);
            file.delete();
        }
        if (!TextUtils.isEmpty(framePicPath)) {
            File file = new File(framePicPath);
            file.delete();
        }
        if(!TextUtils.isEmpty(framePicPath)){
            File file = new File(bitmapName);
            file.delete();
        }
    }
}
